//
//  MessageViewController.m
//  tuanGG
//
//  Created by 微他 on 15/12/3.
//  Copyright © 2015年 weita. All rights reserved.
//

#import "MessageViewController.h"
#import "MessageTableViewCell.h"
@interface Item : NSObject

@property (retain, nonatomic) NSString *title;
@property (assign, nonatomic) BOOL isChecked;

@end

@implementation Item
@end


@interface MessageViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong)UITableView *tableView ;
@property (nonatomic, strong)UIBarButtonItem *rightItem;
@property (nonatomic, strong)UIView *line ;
@property (nonatomic)BOOL formatURL;
@property (nonatomic, strong)MessageTableViewCell *cell;
@property (nonatomic,strong)NSMutableDictionary *dictionary;
@property (nonatomic,strong)NSString *URLstring;
@property (nonatomic, strong)NSMutableArray *array;
@property (nonatomic,assign)NSInteger index ;
@end

@implementation MessageViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    [_tableView headerBeginRefreshing];
    [self tabBarHidden];
    _rightItem = [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(editingItem)];
    NSDictionary *rightTitle = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:14],NSForegroundColorAttributeName:[UIColor whiteColor]};
    [_rightItem setTitleTextAttributes:rightTitle forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = _rightItem;
    _tableView.editing = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initlizeDataSource];

    _formatURL = NO;
    [self addnavigationTitle:@"我的消息" navColor:@"4fd2be"];
    [self Networking];
    [self addTableView];
}
- (void)initlizeDataSource {
    _index = 1;
    [_dictionary addEntriesFromDictionary:@{@"page":[NSString stringWithFormat:@"%ld",_index],@"size":@"10"}];
    _URLstring =  msg_list_url;
    _dataArray = [NSMutableArray array];
}
- (void)initlizeDataSource:(NSArray *)array{

    _array =[NSMutableArray array];
    for (int i = 0 ; i < _dataArray.count;  i++) {
        NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
        [dictionary setObject:@"未选" forKey:@"state"];
        [_array addObject:dictionary];
    }
}
- (void)addTableView {
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0,ScreenWidth, ScreenHeight)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.allowsSelectionDuringEditing = YES;
    _tableView.canCancelContentTouches = YES;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.backgroundColor = UIColorFromRGB(base_gary_bg);
    _tableView.tableFooterView = [[UITableView alloc]init];
    [_tableView registerClass:[MessageTableViewCell class] forCellReuseIdentifier:@"CELL"];
    [self.view addSubview:_tableView];

    _line = [[UIView alloc]initWithFrame:CGRectMake(0, ScreenHeight-64, ScreenWidth, 50)];
    _line.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_line];

    UIButton *endButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth-170, 35)];
    endButton.center = CGPointMake(ScreenWidth/2, _line.frame.size.height/2);
    endButton.backgroundColor = [UIColor redColor];
    [endButton setTitle:@"删除" forState:UIControlStateNormal];
    endButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [endButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [endButton addTarget:self action:@selector(deleteButton) forControlEvents:UIControlEventTouchUpInside];
    endButton.layer.cornerRadius = 5;
    [_line addSubview:endButton];
}
#pragma mark - 事件实现
- (void)editingItem  {
    if (_tableView.editing == NO) {
        [_tableView setEditing:YES animated:YES];
        _rightItem.title = @"取消";
        [UIView animateWithDuration:0.3 animations:^{
            _line.frame = CGRectMake(0, ScreenHeight-114,ScreenWidth, 50);
        }];
    }else if(_tableView.editing == YES) {
        [_tableView setEditing:NO animated:YES];
        _rightItem.title = @"编辑";
        _tableView.editing = NO;

        for (int i = 0 ; i < _array.count; i++) {
            if ([_array[i][@"state"] isEqualToString:@"已选"]) {
                _array[i][@"state"]=@"未选";

            }
        }

        NSLog(@"%@",_array);
        [UIView animateWithDuration:0.3 animations:^{
            _line.frame = CGRectMake(0, ScreenHeight-64,ScreenWidth, 50);
        }];
    }
}
- (void)deleteButton{
//    _URLstring = delectCollection_URL;
    _formatURL = YES;
    _URLstring =  deleate_msg_url;
    _dictionary = [NSMutableDictionary dictionary];
    NSLog(@"单个：%@",_array);
    NSMutableArray *index = [NSMutableArray array];
    NSMutableArray * array = [[NSMutableArray alloc]init];
    for (int i = 0 ; i < _array.count; i++) {
        if ([_array[i][@"state"]isEqualToString:@"已选"]) {
            [index addObject:[NSString stringWithFormat:@"%zi",i]];

        }
    }
    for (NSString *string in index) {
        NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
        [ array addObject:_dataArray[[string integerValue]][@"id"]];
        NSLog(@"已选：%@",dictionary);
    }
    NSMutableString * str= [NSMutableString string];
    for (int i = 0; i < array.count; i ++) {

        if (array.count == 1) {
            str = array[i];
        }
        else
        {
            [str appendString:[NSString stringWithFormat:@"%@a",array[i]]];
        }
    }
    NSLog(@"%@",str);
    [_dictionary setValue:str  forKey:@"notifyids"];
    NSLog(@"字典：%@",_dictionary[@"notifyids"]);
    NSLog(@"删除");
    if (![_dictionary[@"notifyids"]isEqual: @""]) {
        [self deteNetworking];
    }else{
        [Common addAlert:@"没有勾选删除的对象"];
    }
}
- (void)seled {
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(rightItem)];
    NSDictionary *rightTitle = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:15],NSForegroundColorAttributeName:[UIColor whiteColor]};
    [rightItem setTitleTextAttributes:rightTitle forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = rightItem;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    _cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
    _cell.contentLabel.text = _dataArray[indexPath.row][@"content"];
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[_dataArray[indexPath.row][@"dateline"] integerValue]];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *string = [formatter stringFromDate:confromTimesp];
    _cell.timeLabel.text = string;
    [_cell.selectedImage sd_setImageWithURL:nil placeholderImage:IMGNAME(@"未选中状态")];

    if ([_dataArray[indexPath.row][@""]integerValue] == 0) {
        [_cell setChecked:NO];
    }else {
        [_cell setChecked:YES];
    }
    _cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return  _cell;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

    return UITableViewCellEditingStyleNone;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_tableView.editing ) {
        MessageTableViewCell *cell = (MessageTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
        if ([_array[indexPath.row][@"state"]isEqualToString:@"未选"]) {
            _array[indexPath.row][@"state"] = @"已选";
            [cell setChecked:YES];
        }else {
            [cell setChecked:NO];
            _array[indexPath.row][@"state"] = @"未选";
        }
    }
}
- (void)Networking {

    [GLAFRequest startRequest:_URLstring  paramsDic:_dictionary defaultdialog:NO ispost:_formatURL handleSuccess:^(id sta, NSDictionary *paramsDic) {
        NSLog(@"%@",sta);
        if ([sta[@"code"]integerValue] == 1) {
            [_dataArray = sta[@"messageinfo"]mutableCopy];
            [self initlizeDataSource:_dataArray];
            [_tableView reloadData];
        }
    } doFinally:^(NSDictionary *paramsDic) {

    }];
}
- (void)deteNetworking {
    [GLAFRequest startRequest:_URLstring  paramsDic:_dictionary defaultdialog:NO ispost:_formatURL handleSuccess:^(id sta, NSDictionary *paramsDic) {
        NSLog(@"%@",sta);
        if ([sta[@"code"]integerValue] == 1) {
            [_dataArray = sta[@"messageinfo"]mutableCopy];
            [Common addAlert:@"删除成功"];
            [_tableView reloadData];
        }
    } doFinally:^(NSDictionary *paramsDic) {

    }];
}
@end
